package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.wd.health.R;
import com.wd.health.model.adapter.HistoryMoneyAdaptor;
import com.wd.health.model.bean.HistoryMoneyBean;
import com.wd.health.model.bean.MoneyBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.view.activity.RechargeActivity;
import com.wd.health.view.activity.WithdrawActivity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MoneyActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView img_back;
    private TextView wallet_num;
    private Button btn_tixian;
    private Button btn_chongzhi;
    private RecyclerView rv_money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_money);
        initView();
        initData();
        initRv();
    }

    private void initRv() {
        RetrofitManager.getInstance().getHistoryMoneyData(1, 20, new Observer<HistoryMoneyBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(HistoryMoneyBean historyMoneyBean) {
                List<HistoryMoneyBean.ResultBean> list = historyMoneyBean.getResult();
                HistoryMoneyAdaptor historyMoneyAdaptor = new HistoryMoneyAdaptor(list);
                rv_money.setAdapter(historyMoneyAdaptor);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initData() {
        RetrofitManager.getInstance().getMoneyData(new Observer<MoneyBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MoneyBean moneyBean) {
                int result = moneyBean.getResult();
                wallet_num.setText(result+"");
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    private void initView() {
        img_back = findViewById(R.id.img_back);
        wallet_num = findViewById(R.id.wallet_num);
        btn_tixian = findViewById(R.id.btn_tixian);
        btn_chongzhi = findViewById(R.id.btn_chongzhi);
        rv_money = findViewById(R.id.rv_money);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv_money.setLayoutManager(linearLayoutManager);
        img_back.setOnClickListener(this);
        btn_tixian.setOnClickListener(this);
        btn_chongzhi.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_back:
                finish();
                break;
            case R.id.btn_chongzhi:
                Intent intent = new Intent(MoneyActivity.this, RechargeActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_tixian:
                Intent intent02 = new Intent(MoneyActivity.this, WithdrawActivity.class);
                startActivity(intent02);

                break;
        }
    }
}